//
//  CommonTableViewCell.m
//  quanminzhekou
//
//  Created by zxl on 15/1/4.
//  Copyright (c) 2015年 zhongqinglongtu. All rights reserved.
//

#import "CommonTableViewCell.h"
#import "UIImageView+WebCache.h"
#import "CollectManager.h"

@interface CommonTableViewCell ()
@property (nonatomic) UIButton* rightBtn;
@end

@implementation CommonTableViewCell

- (void)awakeFromNib {
    // Initialization code
    
    [super awakeFromNib];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    // Configure the view for the selected state
}

- (void)prepareForReuse {
    [super prepareForReuse];
}

- (void)setIsCollected:(BOOL)isCollected {
    _isCollected = isCollected;
    self.rightBtn.selected = isCollected;
    
    if (_isCollected) {
        [[CollectManager shareManager] addCollectedGoods:self.viewModel];
    } else {
        [[CollectManager shareManager] removeCollectGoods:self.viewModel];
    }
}

- (void)layoutSubviewDataWithViewModel:(CommonCellViewModel*)viewModel {
    self.viewModel = viewModel;
    [self.picView sd_setImageWithURL:viewModel.imgUrl placeholderImage:[UIImage imageNamed:@"commonCell_placeholder"]];
    self.titleLab.text = viewModel.titleStr;
    self.priceLab.text = viewModel.priceStr;
    self.originalPriceLab.attributedText = viewModel.originalPriceStr;
    self.discountLab.text = viewModel.discountStr;
    self.numOfPeopleBuyLab.text = viewModel.numOfPeopleBuyStr;
    self.shootReductionLab.text = viewModel.goodsLableStr;
    self.iconView.hidden = !viewModel.isNew;
    self.tmallIcon.hidden = !(viewModel.goodsSource == CommonCellViewModelGoodsFromTmall);
    
    _isCollected = [[CollectManager shareManager] isCollectedGoods:viewModel];
    
    if (self.isCanSwip && self.isSwiping) {
        [self resetContentViewWithFromPoint:CGPointZero velocity:CGPointZero animating:NO];
    }
}

#pragma mark -
- (UIButton*)rightBtn {
    if (nil == _rightBtn) {
        _rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _rightBtn.frame = CGRectMake(CGRectGetMaxX(self.contentView.frame), 7, 75, 84);
        _rightBtn.backgroundColor = [UIColor colorWithWhite:.92 alpha:1];
        [_rightBtn setImage:[UIImage imageNamed:@"common_collect"] forState:UIControlStateNormal];
        [_rightBtn setImage:[UIImage imageNamed:@"common_collected"] forState:UIControlStateHighlighted];
        [_rightBtn setImage:[UIImage imageNamed:@"common_collected"] forState:UIControlStateSelected];
        [_rightBtn addTarget:self action:@selector(onTapRightBtn:) forControlEvents:UIControlEventTouchUpInside];
        [self.backView addSubview:_rightBtn];
    }
    _rightBtn.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleLeftMargin;
    
    return _rightBtn;
}

- (void)onTapRightBtn:(UIButton*)btn {
    self.isCollected = !btn.selected;
    [self resetContentViewWithFromPoint:CGPointMake(75, 0) velocity:CGPointZero animating:YES];
}

#pragma mark - Super
- (void)didStartSwiping {
    [super didStartSwiping];
    self.isCollected = [[CollectManager shareManager] isCollectedGoods:self.viewModel];
}

- (void)animateContentViewForPoint:(CGPoint)point velocity:(CGPoint)velocity {
    [super animateContentViewForPoint:point velocity:velocity];
    self.rightBtn.frame = CGRectMake(MAX(CGRectGetMaxX(self.frame) - CGRectGetWidth(self.rightBtn.frame), CGRectGetMaxX(self.contentView.frame)), CGRectGetMinY(self.rightBtn.frame), CGRectGetWidth(self.rightBtn.frame), CGRectGetHeight(self.rightBtn.frame));
}

- (void)resetCellFromPoint:(CGPoint)point velocity:(CGPoint)velocity {
    [super resetCellFromPoint:point velocity:velocity];
    if (point.x < 0) {
        if (-point.x <= CGRectGetWidth(self.rightBtn.frame) / 2.0) { //close
            [UIView animateWithDuration:self.animationDuration animations:^{
                self.rightBtn.frame = CGRectMake(CGRectGetMaxX(self.frame), CGRectGetMinY(self.rightBtn.frame), CGRectGetWidth(self.rightBtn.frame), CGRectGetHeight(self.rightBtn.frame));
            } completion:^(BOOL finished) {
                if (self.isDelWhenNotCollect && !self.isCollected && [self.delegate respondsToSelector:@selector(swipeTableViewCellDidResetState:fromPoint:velocity:)]) {
                    [self.delegate swipeTableViewCellDidResetState:self fromPoint:point velocity:velocity];
                }
            }];
        } else {
            [UIView animateWithDuration:self.animationDuration animations:^{ //open
                self.rightBtn.frame = CGRectMake(CGRectGetMaxX(self.contentView.frame), CGRectGetMinY(self.rightBtn.frame), CGRectGetWidth(self.rightBtn.frame), CGRectGetHeight(self.rightBtn.frame));
            }];
        }
    } else {
        [UIView animateWithDuration:self.animationDuration animations:^{ //close
            self.rightBtn.frame = CGRectMake(CGRectGetMaxX(self.frame), CGRectGetMinY(self.rightBtn.frame), CGRectGetWidth(self.rightBtn.frame), CGRectGetHeight(self.rightBtn.frame));
        } completion:^(BOOL finished) {
            if (self.isDelWhenNotCollect && !self.isCollected && [self.delegate respondsToSelector:@selector(swipeTableViewCellDidResetState:fromPoint:velocity:)]) {
                [self.delegate swipeTableViewCellDidResetState:self fromPoint:point velocity:velocity];
            }
        }];
    }
}

-(void)cleanupBackView {
    [_rightBtn removeFromSuperview];
    _rightBtn = nil;
    [super cleanupBackView];
}

@end
